<template> 
  <el-card class="form-container" shadow="never">
    <el-form :model="homeAdvertise"
             :rules="rules"
             ref="homeAdvertiseFrom"
             label-width="150px"
             size="small" class="demo-ruleForm">
      <el-form-item label="套餐名：" prop="name">
        <el-input v-model="homeAdvertise.name" style="width: 200px"></el-input>
      </el-form-item>
      <el-form-item label="​套餐效果轮播图:">
        <multi-upload v-model="selectProductPics"></multi-upload>
      </el-form-item>
      <el-form-item label="​​套餐价：">
        <el-input v-model="homeAdvertise.price" style="width: 200px"></el-input>
        <span style="margin-left: 20px">元</span>
      </el-form-item>
      <el-form-item label="总价折扣：">
        <el-input v-model="homeAdvertise.discount" style="width: 200px"></el-input>
        <span style="margin-left: 20px">%</span>
      </el-form-item>
      <el-form-item label="最低购买价：">
        <el-input v-model="homeAdvertise.basePrice" style="width: 200px"></el-input>
        <span style="margin-left: 20px">元</span>
      </el-form-item>
      <!-- <el-form-item label="VR/短视频：" prop="name">
        <el-select v-model="homeAdvertise.videoType">
          <el-option
            v-for="type in typeOptions"
            :key="type.value"
            :label="type.label"
            :value="type.value">
          </el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="VR链接：" >
        <el-input v-model="homeAdvertise.vrUrl" class="input-width"></el-input>
      </el-form-item>
      <el-form-item label="VR封面图：">
        <single-upload v-model="homeAdvertise.vrCover"></single-upload>
      </el-form-item>
      <el-form-item label="短视频链接：">
        <video-upload v-model="homeAdvertise.video"></video-upload>
      </el-form-item>
      <el-form-item label="短视频封面图：" >
        <single-upload v-model="homeAdvertise.videoCover"></single-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('homeAdvertiseFrom')">提交</el-button>
        <!-- <el-button v-if="!isEdit" @click="resetForm('homeAdvertiseFrom')">重置</el-button> -->
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script>
  import SingleUpload from '@/components/Upload/singleUpload'
  import VideoUpload from '@/components/Upload/videoUpload'
  import MultiUpload from '@/components/Upload/multiUpload'
  import {addcombo, getcombo, updacombo} from '@/api/homeAdvertise'
  const defaultTypeOptions = [
    {
      label: 'VR',
      value: 0
    },
    {
      label: '短视频',
      value: 1
    }
  ];
  const defaultHomeAdvertise = {
    "basePrice": 0,
    "discount":0,
    "name": "",
    "price": 0,
    "video": "",
    "videoCover": "",
    "vrCover": "",
    "vrUrl": "",
    "softoutfitStyleId": 0,
    comboImgList:[]
  };
  export default {
    name: 'HomeAdvertiseDetail',
    components:{SingleUpload, MultiUpload,VideoUpload},
    props: {
      isEdit: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        homeAdvertise: {
          "basePrice": 0,
          "discount":0,
          "name": "",
          "price": 0,
          "video": "",
          "videoCover": "",
          "vrCover": "",
          "vrUrl": "",
          "softoutfitStyleId": 0,
          comboImgList:[]
        },
        rules: {
          name: [
            {required: true, message: '请输入内容', trigger: 'blur'}
          ]
        },
        typeOptions: Object.assign({}, defaultTypeOptions),
      }
    },
    computed: {
    //商品的主图和画册图片
      selectProductPics:{
        get:function () {
          console.log(this.homeAdvertise.comboImgList)
          let pics=[];
          for(let i=0;i<this.homeAdvertise.comboImgList.length;i++){
            pics.push(this.homeAdvertise.comboImgList[i].comboImg);
          }
          return pics;
        },
        set:function (newValue) {
          if (newValue == null || newValue.length === 0) {
                this.homeAdvertise.comboImgList = {
                  comboImg:newValue,
                }
          } else {
            console.log(newValue)
            this.homeAdvertise.comboImgList = [];
            for (let i = 0; i < newValue.length; i++) {
                this.homeAdvertise.comboImgList.push({
                    comboImg:newValue[i],
                })
              }
          }
        }
      }
    },
    created(){
      if (this.isEdit) {
        getcombo(this.$route.query.id).then(response => {
          this.homeAdvertise = response.data;
          this.homeAdvertise.discount = this.homeAdvertise.discount*100
        });
      }else{
        this.homeAdvertise = Object.assign({},defaultHomeAdvertise);
      }
      this.homeAdvertise.softoutfitStyleId = this.$route.query.styleId
    },
    methods: {
      onSubmit(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$confirm('是否提交数据', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              this.homeAdvertise.discount = this.homeAdvertise.discount/100
              if (this.isEdit) {
                updacombo(this.homeAdvertise).then(response => {
                  this.$refs[formName].resetFields();
                  this.$message({
                    message: '修改成功',
                    type: 'success',
                    duration:1000
                  });
                  this.$router.back();
                });
              } else {
                addcombo(this.homeAdvertise).then(response => {
                  this.$refs[formName].resetFields();
                  this.homeAdvertise = Object.assign({},defaultHomeAdvertise);
                  this.$message({
                    message: '提交成功',
                    type: 'success',
                    duration:1000
                  });
                  this.$router.back();
                });
              }
            });
          } else {
            this.$message({
              message: '验证失败',
              type: 'error',
              duration:1000
            });
            return false;
          }
        });
      },
      // resetForm(formName) {
      //   this.$refs[formName].resetFields();
      //   this.homeAdvertise = Object.assign({},defaultHomeAdvertise);
      // }
    }
  }
</script>
<style scoped>
  .input-width {
    width: 70%;
  }
</style>


